﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.GeneralClass
{
    public class ItemDB
    {
        public int ItemID;
        public string ItemName;
        public DateTime ItemTime;
        public string ItemPeople;
    }

      //对项目进行操作
    public class OperateItem
    {
        MyDb mydb = Mdl_Class.Mydb;

        //获取所有项目信息
        public List<ItemDB> GetAllItems()
        {
            List<ItemDB> itemlist = new List<ItemDB>();

            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = "select * from T_B_User";
                    mydb.ExecuteReader(sql);
                    while (mydb.DataReader.Read())
                    {
                        ItemDB itemdb = new ItemDB();
                        itemdb.ItemID = Convert.ToInt32(mydb.DataReader["ItemID"]);
                        itemdb.ItemName = mydb.DataReader["ItemName"].ToString();
                        itemdb.ItemPeople = mydb.DataReader["ItemPeople"].ToString();
                        itemdb.ItemTime = Convert.ToDateTime(mydb.DataReader["ItemTime"]);
                        itemlist.Add(itemdb);
                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetAllItems()", ex.ToString());
                }
            }
            return itemlist;
        }

        //添加项目信息
        public int AddItem(ItemDB item)
        {
            int i = 0;
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("insert into T_B_Item(ItemName,ItemTime,ItemPeople) values('{0}',GetDate(),'{1}')", item.ItemName, item.ItemPeople);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("AddItem(ItemDB item)", ex.ToString());
                }
            }
            return i;
        }

        //修改用户信息
        //public int UpdateUser(User userobj)
        //{
        //    int i = 0;
        //    using (mydb)
        //    {
        //        try
        //        {
        //            mydb.Open();
        //            string sql = string.Format("Update T_B_User set UserName='{0}',PassWord='{1}',userType='{2}' where UserId={3}", userobj.Username, userobj.Password, userobj.Usertype, userobj.Userid);
        //            i = mydb.ExecuteNonQuery(sql);
        //            mydb.Close();
        //        }
        //        catch (Exception ex)
        //        {
        //            Mdl_Class.WriteLog.dbErrLog("UpdateUser(UserModel userobj)", ex.ToString());
        //        }
        //    }
        //    return i;
        //}

        //删除项目信息
        public int DeleteItem(string itemname)
        {
            int i = 0;
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = string.Format("delete from T_B_Item where itemname='{0}'", itemname);
                    i = mydb.ExecuteNonQuery(sql);
                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("DeleteItem(string itemname)", ex.ToString());
                }
            }
            return i;
        }

        //通过条件获取项目
        public ItemDB GetItemDBByItemName(string itemname)
        {
            ItemDB itemdb = null;
            using (mydb)
            {
                try
                {
                    mydb.Open();
                    string sql = "select * from T_B_User";
                    mydb.ExecuteReader(sql);
                    if (mydb.DataReader.Read())
                    {
                        itemdb = new ItemDB();
                        itemdb.ItemID = Convert.ToInt32(mydb.DataReader["ItemID"]);
                        itemdb.ItemName = mydb.DataReader["ItemName"].ToString();
                        itemdb.ItemPeople = mydb.DataReader["ItemPeople"].ToString();
                        itemdb.ItemTime = Convert.ToDateTime(mydb.DataReader["ItemTime"]);

                    }

                    mydb.Close();
                }
                catch (Exception ex)
                {
                    Mdl_Class.WriteLog.dbErrLog("GetItemDBByItemName(string itemname)", ex.ToString());
                }
            }
            return itemdb;
        }
    }
}
